package com.hemaapp.yjnh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.hm_FrameWork.result.HemaPageArrayResult;
import com.hemaapp.hm_FrameWork.view.RefreshLoadmoreLayout;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.adapter.FarmerListAdapter;
import com.hemaapp.yjnh.bean.Client;
import com.hemaapp.yjnh.bean.User;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import xtom.frame.util.XtomToastUtil;
import xtom.frame.view.XtomListView;
import xtom.frame.view.XtomRefreshLoadmoreLayout;

public class ApplyFarmerActivity0 extends BaseActivity {

    @Bind(R.id.tv_apply)
    TextView tvApply;
    @Bind(R.id.tv_apply_info)
    TextView tvApplyInfo;
    @Bind(R.id.tv_farmer_introduce)
    TextView tvFarmerIntroduce;
    private TextView titleText;
    private ImageButton titleLeft;
    private Button titleRight;

    private RefreshLoadmoreLayout refreshLayout;
    private View headerView;
    private XtomListView listview;

    private int curPage = 0;
    private FarmerListAdapter farmerListAdapter;
    private ArrayList<Client> farmers = new ArrayList<>();//最新田家

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_apply_farmer0);
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        getNetWorker().clientList("", "1", "0", "1", "2", "", curPage+"", "", "");
        //初始化listivew的header部分
        headerView = LayoutInflater.from(mContext).inflate(R.layout.header_apply_farmer0, null);
        ButterKnife.bind(this, headerView);
        listview.addHeaderView(headerView);
    }

    @Override
    protected void findView() {
        titleText = (TextView) findViewById(R.id.title_text);
        titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
        titleRight = (Button) findViewById(R.id.title_right_btn);

        refreshLayout = (RefreshLoadmoreLayout) findViewById(R.id.refreshLoadmoreLayout);
        listview = (XtomListView) findViewById(R.id.listview);
    }

    @Override
    protected void setListener() {
        titleText.setText("我的田家");
        titleLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        refreshLayout.setOnStartListener(new XtomRefreshLoadmoreLayout.OnStartListener() {
            @Override
            public void onStartRefresh(XtomRefreshLoadmoreLayout xtomRefreshLoadmoreLayout) {
                curPage = 0;
                getNetWorker().clientList("", "1", "0", "1", "1", "", curPage+"", "", "");
            }

            @Override
            public void onStartLoadmore(XtomRefreshLoadmoreLayout xtomRefreshLoadmoreLayout) {
                curPage++;
                getNetWorker().clientList("", "1", "0", "1", "1", "", curPage+"", "", "");
            }
        });
    }
    @OnClick({R.id.tv_apply, R.id.tv_apply_info, R.id.tv_farmer_introduce})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_apply:
//                Intent itApply = new Intent(mContext, ApplyFarmerActivity.class);
//                itApply.putExtra("type", 1);
//                startActivity(itApply);
                User user = getApplicationContext().getUser();
                Intent itFarmer = new Intent();
                if (user.getRole().equals("1")) {//角色	0普通用户 1田家用户
                    itFarmer.setClass(mContext, MyFarmerActivity.class);
                } else if (user.getAuthflag().equals("1")) {//审核中的普通用户
                    itFarmer.setClass(mContext, FarmerProgressActivity.class);
                } else {//普通田家
                    itFarmer.setClass(mContext, ApplyFarmerActivity.class);
                    itFarmer.putExtra("type", 1);
                }
                startActivity(itFarmer);
                break;
            case R.id.tv_apply_info:
                Intent itInfo = new Intent(mContext, WebviewActivity.class);
                itInfo.putExtra("type", 13);
                itInfo.putExtra("parm", "farmer_auth");
                startActivity(itInfo);
                break;
            case R.id.tv_farmer_introduce:
                Intent itIntroduce = new Intent(mContext, WebviewActivity.class);
                itIntroduce.putExtra("type", 14);
                itIntroduce.putExtra("parm", "farmer_intro");
                startActivity(itIntroduce);
                break;
        }
    }

    @Override
    protected void callBeforeDataBack(HemaNetTask netTask) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case CLIENT_LIST:
                showProgressDialog("请稍候");
            default:
                break;
        }
    }

    @Override
    protected void callAfterDataBack(HemaNetTask netTask) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case CLIENT_LIST:
                cancelProgressDialog();
            default:
                break;
        }
    }
    @Override
    protected void callBackForServerSuccess(HemaNetTask netTask,
                                            HemaBaseResult baseResult) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case CLIENT_LIST:
                HemaPageArrayResult<Client> cResult = (HemaPageArrayResult<Client>) baseResult;
                if (cResult == null || cResult.getObjects() == null)
                    return;
                ArrayList<Client> clients = cResult.getObjects();
                if (curPage == 0) {// 刷新
                    refreshLayout.refreshSuccess();
                    refreshLayout.setLoadmoreable(true);
                    farmers.clear();
                    farmers.addAll(clients);
                } else {
                    refreshLayout.loadmoreSuccess();
                    if (clients.size() > 0)
                        this.farmers.addAll(clients);
                    else {
                        refreshLayout.setLoadmoreable(false);
                        XtomToastUtil.showShortToast(mContext, "已经到最后啦");
                    }
                }
                refreshHomePage(-1);
                break;
        }
    }
    @Override
    protected void callBackForServerFailed(HemaNetTask netTask,
                                           HemaBaseResult baseResult) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case CLIENT_LIST:
                if (curPage == 0) {
                    refreshLayout.refreshFailed();
                }else {
                    refreshLayout.loadmoreFailed();
                }
                XtomToastUtil.showShortToast(mContext, "获取信息失败，"+baseResult.getMsg());
                break;
            default:
                break;
        }
    }

    @Override
    protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case CLIENT_LIST:
                if (curPage == 0) {
                    refreshLayout.refreshFailed();
                }else {
                    refreshLayout.loadmoreFailed();
                }
                XtomToastUtil.showShortToast(mContext, "获取信息失败，请稍后再试");
                refreshHomePage(failedType);
                break;
            default:
                break;
        }
    }

    private void refreshHomePage(int failedType) {
        if (null == farmerListAdapter) {
            farmerListAdapter = new FarmerListAdapter(mContext, farmers);
            farmerListAdapter.setEmptyString("暂时没有推荐田家");
            listview.setAdapter(farmerListAdapter);
        }else {
            farmerListAdapter.notifyDataSetChanged();
        }
        farmerListAdapter.setFailtype(failedType);
    }
}
